<script lang="ts" setup>
import { House, ArrowRight } from "@element-plus/icons-vue"
import { onMounted, onUnmounted, reactive, ref } from "vue";
import { useRouter } from 'vue-router';
import createAxios from "../request";
import { ElMessage } from "element-plus";
import userStore from '../store/UserStore';

let store = userStore();
let router = useRouter();
onMounted(() => {
  today.value = getCurrentDate();
  searchClock();
});

let searchClock = () => {
  let axios = createAxios();
  axios({
    url: "/searchClock",
    method: "POST",
    data: {
      date: today.value
    }
  }).then(res => {
    tableData[0].startTime = res.data.clockIn_time;
    tableData[0].endTime = res.data.clockOut_time;
  }).catch(err => {
    ElMessage.error("访问失败");
  })
}

const tableData = reactive([
  {
    "name": store.getAccount,
    "startTime": '',
    "endTime": ''
  }
])

let jump4_1_1 = function () { router.push('/home/supplement') };

let currentDate = new Date();
let year = currentDate.getFullYear();
let month = currentDate.getMonth() + 1; // 月份是从0开始的
let date = currentDate.getDate();
let hours = currentDate.getHours();
let minutes = currentDate.getMinutes();
let seconds = ref(currentDate.getSeconds());
let timeclear = function () {
  let currentDate = new Date();
  year = currentDate.getFullYear();
  month = currentDate.getMonth() + 1; // 月份是从0开始的
  date = currentDate.getDate();
  hours = currentDate.getHours();
  minutes = currentDate.getMinutes();
  seconds.value = currentDate.getSeconds();
}

onMounted(() => {
  setInterval(timeclear, 1000);
});


// ======================================================
// 获取当前日期
const today = ref('');

onMounted(() => {
  today.value = getCurrentDate();
});

function getCurrentDate() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}
// ======================================================

let clockIn = () => {
  let axios = createAxios();
  axios({
    url: "/clockin",
    method: "POST",
    data: {
      date: today.value,
      clockIn_time: hours + ":" + minutes + ":" + seconds.value
    }
  }).then(res => {
    if (res.data == '1') {
      ElMessage.success("上班打卡成功");
      searchClock();
    } else if (res.data == '0') {
      ElMessage.error("重复打卡");
    }
  }).catch(err => {
    ElMessage.error("访问失败");
  })
}

const clockOut = () => {
  let axios = createAxios();
  axios({
    url: "/clockout",
    method: "POST",
    data: {
      date: today.value,
      clockOut_time: hours + ":" + minutes + ":" + seconds.value
    }
  }).then(res => {
    console.log(res.data);
    if (res.data == '1') {
      ElMessage.success("下班打卡成功");
      searchClock();
    } else {
      ElMessage.error("重复打卡");
    }
  }).catch(err => {
    ElMessage.error("访问失败");
  })
}
</script>

<template>
  <el-container id="mainView">
    <el-header style="background-color: #272822; color: #E5E5E5; line-height: 60px; font-size: 27px;">考勤打卡</el-header>
    <el-footer
      style="height: 30px; line-height: 30px; background-color: #4E5246; border-bottom:1px solid #7A7A7A; border-top:1px solid #7A7A7A;">
      <el-breadcrumb :separator-icon="ArrowRight" style="line-height: 30px;">
        <el-breadcrumb-item :to="{ path: '/home/homepage' }"><el-icon>
            <House />
          </el-icon>首页</el-breadcrumb-item>
        <el-breadcrumb-item>时间管理</el-breadcrumb-item>
        <el-breadcrumb-item>考勤管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-footer>
    <el-main style="padding-bottom: 0%;">
      <div class="l1">
        <div class="title">当前服务器时间</div>
        <div class="time">
          <div class="timeblock">
            <div>{{ year }}</div>
            <div style="color: white;">年</div>
          </div>
          <div class="timeblock">
            <div>{{ month }}</div>
            <div style="color: white;">月</div>
          </div>
          <div class="timeblock">
            <div>{{ date }}</div>
            <div style="color: white;">日</div>
          </div>
          <div class="timeblock">
            <div>{{ hours }}</div>
            <div style="color: white;">时</div>
          </div>
          <div class="timeblock">
            <div>{{ minutes }}</div>
            <div style="color: white;">分</div>
          </div>
          <div class="timeblock">
            <div>{{ seconds }}</div>
            <div style="color: white;">秒</div>
          </div>
        </div>
      </div>
      <div>
        <el-card style="width: 100%; text-align: center;">
          <div>
            <el-button type="primary" @click="clockIn" class="btn">上班打卡</el-button>
            <el-button type="primary" @click="clockOut" class="btn">下班打卡</el-button>
            <el-button type="primary" class="btn" @click="jump4_1_1">补签</el-button>
          </div><br>
          <div class="table">
            <el-table :data="tableData" border style="width: 80%;">
              <el-table-column prop="name" label="打卡人" class="item" />
              <el-table-column prop="startTime" label="上班时间" class="item" />
              <el-table-column prop="endTime" label="下班时间" class="item" />
            </el-table>
          </div>
        </el-card>
      </div>
    </el-main>
  </el-container>
</template>

<style scoped>
#mainView {
  height: 100%;
  width: 100%;
}

.btn {
  width: 20%;
}

.table {
  display: flex;
  justify-content: center;
}

.item {
  width: 25%;
}

.l1 {
  width: 100%;
  background-image: url("../img/1.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  /* background-color: #777; */
  /* height: 215px; */
}

.timeblock {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background-color: rgb(104, 103, 103, 0.5);
  margin: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.timeblock div:not(:last-child) {
  font-size: 30px;
  color: white;
}

.time {
  display: flex;
  justify-content: center;
  /* padding: 50px; */
}

.title {
  text-align: center;
  padding-top: 20px;
  color: white;
  font-size: 32px;
  font-weight: 700;
  text-transform: uppercase;
  text-shadow: 0 1px 7px rgb(0 0 0 / 20%);
}
</style>